package com.dart.sys.mq.consumer.queue;

import javax.jms.JMSException;
import javax.jms.MapMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import com.dart.sys.entity.SysExLog;
import com.dart.sys.entity.SysOperateLog;
import com.dart.sys.service.SysExLogService;
import com.dart.sys.service.SysOperateLogService;

/**
 * 系统操作日志消息监听类
 * 
 * @作者：杨浩泉
 * @日期：2016年9月30日
 */
@Component
public class LogHandlerQueueReceiver {
	
	private static final Logger logger = LoggerFactory.getLogger(LogHandlerQueueReceiver.class) ;
	
	@Autowired
	private SysOperateLogService operateLogService ;
	
	@Autowired
	private SysExLogService exLogService ;
	
	@JmsListener(destination = "SysOperateLog")
	public void receiveLogOperateMessage(MapMessage mapMessage) throws JMSException {
		SysOperateLog entity = new SysOperateLog() ;
		entity.setOperate(mapMessage.getString("operate"));
		entity.setClassName(mapMessage.getString("className"));
		entity.setMethodName(mapMessage.getString("methodName"));
		entity.setParams(mapMessage.getString("params"));
		entity.setIp(mapMessage.getString("ip"));
		entity.setDetail(mapMessage.getString("detail"));
		
		this.operateLogService.insertLog(entity);
		
		logger.info("用户操作记录，操作说明：{}, 类名：{}, 方法：{}, 参数：{}, IP：{}, 详情：{}", entity.getOperate(), entity.getClassName(), entity.getMethodName(), entity.getParams(), entity.getIp(), entity.getDetail());
	}
	
	@JmsListener(destination = "SysExLog")
	public void receiveLogExMessage(MapMessage mapMessage) throws JMSException {
		SysExLog entity = new SysExLog() ;
		entity.setOperate(mapMessage.getString("operate"));
		entity.setClassName(mapMessage.getString("className"));
		entity.setMethodName(mapMessage.getString("methodName"));
		entity.setParams(mapMessage.getString("params"));
		entity.setException(mapMessage.getString("exception"));
		entity.setIp(mapMessage.getString("ip"));
		entity.setDetail(mapMessage.getString("detail"));
		
		this.exLogService.insertLog(entity);
		
		logger.error("用户操作异常，操作说明：{},操作详情：{} \r\n异常详情：{}", entity.getOperate(), entity.getDetail(), entity.getException());
	}

}
